package dk.dtu.student.assignmentfour;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphView.LegendAlign;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewStyle;
import com.jjoe64.graphview.LineGraphView;

public class GraphActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_graph);
		
		LinearLayout allLayout = (LinearLayout) findViewById(R.id.overallGraphView);
		LinearLayout mineLayout = (LinearLayout) findViewById(R.id.yoursGraphView);
		LinearLayout notMineLayout = (LinearLayout) findViewById(R.id.hisGraphView);

		Bundle b = getIntent().getExtras();
		String mThreadId = b.getString("mThreadId");

		MyApplication app = (MyApplication)getApplication();		
		for (SmsThread s : app.mSmsThreads) {
			if (s.getThreadId().equals(mThreadId)) {
				Log.i("GraphActivity", "MATCH! Number: " + s.getContactName());
				
				this.setTitle(s.getContactName());
				
				Afinn finn;
				try {
					finn = new Afinn(this);		

					int num = s.getSmsCount();
					GraphViewData[] data = new GraphViewData[num];
					GraphViewData[] dataMine = new GraphViewData[num];
					GraphViewData[] dataNotMine = new GraphViewData[num];
					
					List<SmsMessage> messages = new ArrayList<SmsMessage>(s.getAllSmsMessages());
					Collections.reverse(messages);
					int i = 0;
					for (SmsMessage m : messages){
						double mood = finn.sentiment(m.getMessageBody());
						data[i] = new GraphViewData(i, mood);
												
						if (m.getIfMine())
							dataMine[i] = new GraphViewData(i, mood);
						else
							dataNotMine[i] = new GraphViewData(i, mood);
						
						i++;
					}
					
					List<GraphViewData> list = new ArrayList<GraphViewData>();

				    for(GraphViewData ss : dataMine) {
				       if(ss != null) {
				          list.add(ss);
				       }
				    }
				    
				    dataMine = list.toArray(new GraphViewData[list.size()]);
				    
				    list = new ArrayList<GraphViewData>();
				    
				    for(GraphViewData ss : dataNotMine) {
				       if(ss != null) {
				          list.add(ss);
				       }
				    }
				    dataNotMine = list.toArray(new GraphViewData[list.size()]);
					
					GraphViewSeries seriesAll = new GraphViewSeries("Mood", new GraphViewStyle(Color.rgb(90, 250, 0), 3), data);
					GraphViewSeries seriesMine = new GraphViewSeries("Mood", new GraphViewStyle(Color.rgb(250, 90, 0), 3), dataMine);
					GraphViewSeries seriesNotMine = new GraphViewSeries("Mood", new GraphViewStyle(Color.rgb(0, 90, 250), 3), dataNotMine);

					// create graph  
					GraphView allGraphView = createGraphView(this, "All messages");
					GraphView mineGraphView = createGraphView(this, "My messages");
					GraphView notMineGraphView = createGraphView(this, "Recipients messages");

					// add data  
					allGraphView.addSeries(seriesAll);
					mineGraphView.addSeries(seriesMine);
					notMineGraphView.addSeries(seriesNotMine);
					
					if (num > 100) {
						allGraphView.setViewPort(0, 100);
						mineGraphView.setViewPort(0, 100);
						notMineGraphView.setViewPort(0, 100);
					} else {
						allGraphView.setViewPort(0, num);
						mineGraphView.setViewPort(0, num);
						notMineGraphView.setViewPort(0, num);
					}

					// set labels
					String[] hLabels = new String[] {"messages"};
					String[] vLabels = new String[] {"high", "middle", "low"};
					allGraphView.setHorizontalLabels(hLabels);
					allGraphView.setVerticalLabels(vLabels);
					mineGraphView.setHorizontalLabels(hLabels);
					mineGraphView.setVerticalLabels(vLabels);
					notMineGraphView.setHorizontalLabels(hLabels);
					notMineGraphView.setVerticalLabels(vLabels);

					allLayout.addView(allGraphView);
					mineLayout.addView(mineGraphView);
					notMineLayout.addView(notMineGraphView);
				} catch (IOException e) {
					Log.w("GraphActivity", "Afinn failed man!");
				}
			}
		}		
	}
	
	public GraphView createGraphView(Context context, String title)
	{
		GraphView graphView = new LineGraphView(context, title);
				
		graphView.setScalable(true);
		graphView.setManualYAxis(true);
		graphView.setManualYAxisBounds(4.0, -4.0);
		graphView.setShowLegend(true);
		graphView.setLegendAlign(LegendAlign.BOTTOM);
		graphView.setLegendWidth(200);
		
		graphView.setBackgroundColor(Color.argb(90, 0, 0, 0));
		
		return graphView;
	}
}
